package com.ax.center.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.ax.center.dto.ApiResponse;
import com.ax.center.entity.UserAuth;
import com.ax.center.entity.UserRole;
import com.ax.center.entity.Users;
import com.ax.center.mapper.UserAuthMapper;
import com.ax.center.mapper.UsersMapper;
import com.ax.center.service.UserAuthService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @date 2023/7/5 15:22
 */
@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper,UserAuth> implements UserAuthService {
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserAuthMapper userAuthMapper;

    @Override
    public  List<Long> queryAuthByUserId(Long userId) {
        List<UserAuth> userAuths = query().eq("user_id", userId).list();
        List<Long> authorityIds = userAuths.stream()
                .map(UserAuth::getAuthorityId)
                .collect(Collectors.toList());
        return authorityIds;
    }

    @Override
    public ApiResponse deleteById(Long staffId,Long authId) {
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("staff_id",staffId);
        Users users = usersMapper.selectOne(usersQueryWrapper);
        Long usersId = users.getId();
        QueryWrapper<UserAuth> userAuthQueryWrapper = new QueryWrapper<>();
        QueryWrapper<UserAuth> userAuthQueryWrapper1 = userAuthQueryWrapper.eq("user_id", usersId).eq("authority_id", authId);
        int delete = userAuthMapper.delete(userAuthQueryWrapper1);
        if(delete==1){
            return ApiResponse.ok("删除成功");
        }else {
            return ApiResponse.fail("删除失败");
        }
    }

    @Override
    public ApiResponse deleteByAuthId(Long authId) {
        QueryWrapper<UserAuth> userAuthQueryWrapper = new QueryWrapper<>();
        userAuthQueryWrapper.eq("authority_id",authId);
        remove(userAuthQueryWrapper);
        return ApiResponse.ok("删除成功");
    }

    @Override
    public ApiResponse insertById(Long staffId, Long authId) {
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("staff_id",staffId);
        Users users = usersMapper.selectOne(usersQueryWrapper);
        Long usersId = users.getId();
        UserAuth userAuth = new UserAuth();
        userAuth.setUserId(usersId);
        userAuth.setAuthorityId(authId);
        boolean save = save(userAuth);
        if(BooleanUtil.isTrue(save)){
            return ApiResponse.ok("添加成功");
        }else {
            return ApiResponse.fail("添加失败");
        }
    }

    @Override
    public ApiResponse deleteByUserId(Long userId) {
        QueryWrapper<UserAuth> userAuthQueryWrapper = new QueryWrapper<>();
        userAuthQueryWrapper.in("user_id",userId);
        userAuthMapper.delete(userAuthQueryWrapper);
        return ApiResponse.ok("删除成功");
    }


}
